package com.vend.common.loading;

import android.app.Activity;
import android.util.Log;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;

/**
 * Created by glendal on 2022/01/10.
 * dialog loading 弹窗
 */
public class LoadingTools {
    private static LoadingPopupView load;
    private static Activity lastContext;

    public static void showLoading(Activity context, String str) {
//        Log.e("tag","显示加载Loading 1" );
        if ((lastContext != null && lastContext.isFinishing()) || (lastContext != null && !lastContext.isFinishing() && !lastContext.getClass().equals(context.getClass()))){
//            Log.e("tag","影藏 loading 1");
            hide();
        }
        str = str.trim();
        if (load == null && !context.isFinishing()) {
            load = new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .hasNavigationBar(false)
                    .hasStatusBar(false)
                    .dismissOnBackPressed(false)
                    .setPopupCallback(new XPopupCallback() {
                        @Override
                        public void onCreated(BasePopupView popupView) {

                        }

                        @Override
                        public void beforeShow(BasePopupView popupView) {

                        }

                        @Override
                        public void onShow(BasePopupView popupView) {

                        }

                        @Override
                        public void onDismiss(BasePopupView popupView) {

                        }

                        @Override
                        public void beforeDismiss(BasePopupView popupView) {

                        }

                        @Override
                        public boolean onBackPressed(BasePopupView popupView) {
                            return false;
                        }

                        @Override
                        public void onKeyBoardStateChanged(BasePopupView popupView, int height) {

                        }

                        @Override
                        public void onDrag(BasePopupView popupView, int value, float percent, boolean upOrLeft) {

                        }

                        @Override
                        public void onClickOutside(BasePopupView popupView) {

                        }
                    })
                    .asLoading(str);
            lastContext = context;
        } else {
            load.setTitle(str);
        }
        load.show();
    }

    /**
     * 显示loading
     * @param context 上下文
     * @param str 文本
     * @param isDimissOnTouchOutSide 点击外部是否显示
     */
    public static void showLoading(Activity context, String str, boolean isDimissOnTouchOutSide) {
//        Log.e("tag","显示加载Loading 2");
        if ((lastContext != null && lastContext.isFinishing()) || (lastContext != null && !lastContext.isFinishing() && !lastContext.getClass().equals(context.getClass()))){
//            Log.e("tag","影藏 loading 2");
            hide();
        }
        if (load == null && !context.isFinishing()) {
            load = new XPopup.Builder(context)
                    .dismissOnTouchOutside(isDimissOnTouchOutSide)
                    .hasNavigationBar(false)
                    .hasStatusBar(false)
                    .dismissOnBackPressed(false)
                    .asLoading(str);
        } else {
            load.popupInfo.isDismissOnTouchOutside = isDimissOnTouchOutSide;
            load.setTitle(str);
        }
        load.show();
    }

    /**
     * 关闭
     */
    public static void hide() {
//        Log.e("tag","hide1");
        if (load != null && !load.isDismiss()) {
//            Log.e("tag","真的影藏Loading");
            load.dismiss();
        }
        load = null;
    }

    /**
     * 界面渲染完成关闭后跳转
     * @param r runable
     */
    public static void hide(Runnable r){
//        Log.e("tag","hide2");
        if (load != null && !load.isDismiss()){
            load.dismissWith(r);
        }
        load = null;
    }

    /**
     * 最短时间显示
     */
    public static void hideMine(){
//        Log.e("tag","hide3");
        if (load != null && !load.isDismiss()){
            load.smartDismiss();
        }
        load = null;
    }


}
